<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_MAP_GROUP.php' );
require_once( '../system/mdao/Class_M_MAP_GROUP_WEEK.php' );
require_once( '../system/mdao/Class_M_MATRIX.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_MAP_GROUP,$login['staff_auth']);

$isG7  = FALSE;
if   ($login['staff_auth'] == 'G-7') {
	$isG7 = TRUE;
}

$dao =  new Class_M_MAP_GROUP;
$daoW = new Class_M_MAP_GROUP_WEEK($dao->get_db());
$dao1 =  new Class_M_MATRIX;
$map_arr = $dao->get_all_mapno();
$pattern = Class_M_OTHER::get_pattern_array(PLEASE_SELECT_ARR, 23);

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';
	$dataiterm           = $dao1->get( $_GET['week_cd'] );
	
	$forms["matrix_cd"]  = $_GET['week_cd'];
	$forms["year4"]      = $dataiterm["year4"];
	$forms["week_cd"]    = $dataiterm["week_cd"];
	$forms["week_seq"]   = $dataiterm["week_seq"];
	$forms["flier_date"] = Class_PWD::int8_to_dateD($dataiterm["flier_date"]) ;
	
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$formsa  = $dao->get( $_GET['id'] );
		
		$forms["id"]       = $_GET['id'];
		foreach ($formsa as $k => $v) {
			$forms[$k] =   $v;
		}
		
	}else{
		//$forms['auth'] = 0;
	}
}else{
	$forms = $_POST;
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
	
		if ( $err->clear ){
			$cols     = $dao->get_cols();
		}
		foreach($cols as $kcol => $val) {
			$values[$kcol] = $forms[$kcol] ;
		}
		$where          = sprintf(" map_no = '%s' ", $forms['map_no'] );
		$not_exits_map  = true;
		if ( $dao->exits($where) ) {
			$not_exits_map  = false; 
			
		}
		
		if( $forms['id'] == '' ){
			if ($not_exits_map== false) {
				$err->clear     = false;
				$err_mes['map_no'] = '<p class="error">このマップNOは重複しています</p>';
			}
		} else {
			if ($not_exits_map== true) {
				$err->clear     = false;
				$err_mes['map_no'] = '<p class="error">このマップNOは存在しません</p>';
			}
		}
		
		$err_mes["sort"] = $err->check( $values["sort"], array("EXIST") );
			
	
		
		if  ($values["sort"] == 3 ) {
			$err_mes["media_cd"] = $err->check( $values["media_cd"], array("EXIST") );
			
		}
		
		
		//桁数チェック
		$maxlens = $dao->get_maxlens();
		foreach ( $maxlens as $k => $v ) {
			$err_mes[$k] =$err->check_size($values[$k], $v);
		}
		
		if( $err->clear ){
			if  ($values["sort"] == 3 ) {
				//$values["media_cd"]      =  class_M_OTHER::do_media($values["media_name"]);
				$values["media_name"]      =  $pattern[$values["media_cd"]];
			}else {
				$values["media_name"]    = "";
				$values["media_cd"]      = 0;
			}
			
			$map_upd  = true;
			$dao->begin_trans();
			if( $forms['id'] == '' ){
				if ($dao->add0($values)) {
					
				} else {
					$map_upd = false;
				}
			}else {
				if ($dao->edit( $forms['id'], $values )) {
				} else {
					$map_upd = false;
				}
			}
			
			if ($map_upd) {
				$where = sprintf(" map_no = '%s' AND matrix_cd  = '%s'", $forms['map_no'],$forms['matrix_cd'] );
				$values = array();
			
				$values["map_no"]    = $forms["map_no"] ;
				$values["matrix_cd"] = $forms["matrix_cd"] ;
				
				if ( $daoW->exits($where) ) {
					//if ($daoW->editbywhere( $where, $values )) {
						$smarty->assign('message', '更新が完了しました。');
						$dao->commit_trans();
					//} else {
					//	$smarty->assign('message', '更新が失敗しました。');
					//	$dao->rollback_trans();
					//}
				} else {
					if ($daoW->add0($values)) {
						$smarty->assign('message', '更新が完了しました。');
						$dao->commit_trans();
					} else {
						$smarty->assign('message', '更新が失敗しました。');
						$dao->rollback_trans();
					}
				}
			}else {
				$smarty->assign('message', '更新が失敗しました。');
				$dao->rollback_trans();
			}
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);

if ($phase != 'complete') {
	
}
$smarty->assign('pattern', $pattern );
$smarty->assign('map_sort', $aoki_map_sort );
$smarty->assign('map_arr', implode (',' , $map_arr ) );
$smarty->assign('isG7', $isG7 );
$smarty->assign('phase', $phase );
$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mmapg_form.html');
?>